package com.xiaren.ui.dialog;

import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

import com.sun.awt.AWTUtilities;
import com.xiaren.biz.WindowFloatEvent;
import com.xiaren.common.IConstant;
import com.xiaren.common.ObjectConstant;

/**
 * 软件 的图标窗体
 *
 * @author xiaren
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 *
 */
public class TitleWindow extends JWindow {

	private JPanel imagePanel;

	private ImageIcon background;

	private static TitleWindow titleWindow = new TitleWindow();

	public static synchronized TitleWindow creatTitleWindow() {
		return titleWindow;
	}

	private TitleWindow() {
		super();
		init();
		ObjectConstant.titleWindow = this;
		this.addMouseListener(WindowFloatEvent.getInstance());
		this.addMouseMotionListener(WindowFloatEvent.getInstance());
	}

	void init() {
		AWTUtilities.setWindowOpaque(this, false);
		background = new ImageIcon(IConstant.FLOATIMAGE);// 背景图片
		JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
		// 把标签的大小位置设置为图片刚好填充整个面板
		label.setBounds(0, 0, background.getIconWidth(),
				background.getIconHeight());
		// 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
		imagePanel = (JPanel) this.getContentPane();
		imagePanel.setOpaque(false);
		// 内容窗格默认的布局管理器为BorderLayout
		imagePanel.setLayout(new FlowLayout());

		this.getLayeredPane().setLayout(null);
		// 把背景图片添加到分层窗格的最底层作为背景
		this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
		this.setSize(background.getIconWidth(), background.getIconHeight());
		this.setLocation(
				ObjectConstant.screenWidth - background.getIconWidth() - 50, 50);
		setAlwaysOnTop(true);
		this.setVisible(true);
	}

}
